<template>
  <div class="container">
    <el-card class="table-container">
      <div style="margin: 0 10px 20px 10px">
        <el-form :inline="true" ref="searchForm" :model="searchForm" class="demo-form-inline">
          <!-- <el-form-item label="医院" prop="hospitalId">
            <el-col :span="22">
              <el-input :rows="5" v-model="searchForm.hospitalId" clearable type="text" placeholder="请输入医院id" />
            </el-col>
          </el-form-item> -->
          <el-form-item label="医院" prop="hospitalId">
            <el-col :span="22">
              <el-select style="width: 100%;" v-model="searchForm.hospitalId"
                placeholder="请选择医院">
                <el-option v-for="item in hospitalOptions" :key="item.hospitalId" :label="item.hospitalName"
                  :value="item.hospitalId" />
              </el-select>
            </el-col>
          </el-form-item>
          <!-- <el-form-item label="性别" prop="gender" label-width="80px">
            <el-col :span="22">
              <el-input :rows="5" v-model="searchForm.gender" clearable type="text" placeholder="请输入性别" />
            </el-col>
          </el-form-item> -->
          <el-form-item label="金额" prop="amount" label-width="80px">
            <el-col :span="22">
              <el-input :rows="5" v-model="searchForm.amount" clearable type="text" placeholder="请输入年龄分组" />
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="resetForm">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd">新增</el-button>
          </el-form-item>

          <!-- <el-form-item>
            <el-button :loading="exportLoading" type="success" plain @click="handleExportExcel">Excel导出</el-button>
          </el-form-item> -->
        </el-form>
      </div>
      <el-table :data="tableData.list" :header-row-class-name="'table-head-th'" row-key="id" fit
        v-loading="queryLoading" highlight-current-row>
        <!-- <el-table-column
        key="hospitalId"
        show-overflow-tooltip
        prop="hospitalId"
        label="医院id"/> -->
        <el-table-column align="center" key="hospitalName" show-overflow-tooltip prop="hospitalName" label="医院" />
        <el-table-column align="center" key="gender" show-overflow-tooltip prop="gender" label="性别" />
        <el-table-column align="center" key="ageGroup" show-overflow-tooltip prop="ageGroup" label="年龄分组" />
        <el-table-column align="center" key="amount" show-overflow-tooltip prop="amount" label="金额" />
        <el-table-column align="center" key="maxSelection" show-overflow-tooltip prop="maxSelection" label="最大人数" />
        <el-table-column align="center" key="currentSelection" show-overflow-tooltip prop="currentSelection"
          label="当前人数" />
        <!-- 表格操作列 -->
        <el-table-column label="操作" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="mini" title="查看" type="primary" @click="handleDetail(scope.row)">
                <i class="fa fa-eye" />
              </el-button>
              <el-button size="mini" title="编辑" type="primary" @click="handleEdit(scope.row)">
                <i class="fa fa-pencil" />
              </el-button>
              <el-button size="mini" title="删除" type="danger" @click="handleDel(scope.row)">
                <i class="fa fa-trash-o" />
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页信息 -->
      <div class="pagination-container">
        <el-pagination background layout="total, prev, pager, next, jumper" @current-change="handlePageChange"
          @size-change="handleSizeChange" :current-page="pageNum" :total="tableData.total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="mode === 'add' ? '新增' : '编辑'" width="30%" :visible.sync="showEditDialog" :before-close="handleDialogClose">
      <examination-packages-edit :mode="mode" ref="editForm" :record="currentRecord" @after-edit="handleAfterEdit" />
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import ExaminationPackagesApi from '../api/examinationPackages'
import examinationPackagesEdit from './examinationPackagesEdit'
import ImportExcel from '@/components/ImportExcel'
import HospitalsApi from '../../hospitals/api/hospitals'
export default {
  name: 'ExaminationPackagesList',
  components: {
    examinationPackagesEdit,
    ImportExcel
  },
  data() {
    return {
      /* 搜索请求数据*/
      hospitalOptions: [],
      searchForm: {
        hospitalId:"",
        amount:""
      },
      pageSize: 10,
      pageNum: 1,
      // 表格数据
      tableData: {
        results: []
      },
      mode: 'edit',
      // 是否显示添加/编辑对话框
      showEditDialog: false,
      // 当前处理行
      currentRecord: {},
      queryLoading: false,
      exportLoading: false,
      autoWidth: true,
      bookType: 'xlsx',

    }
  },
  mounted() {
    this.init()
    HospitalsApi.queryByCond({}, 100, 1).then(res => {
      this.hospitalOptions = res.data.data.list
    })
  },
  methods: {
    resetForm() {
      this.$refs.searchForm.resetFields()
      this.searchForm={
        hospitalId:undefined,
        amount:undefined
      },
      this.handleQuery()
    },
    // 页面初始化
    init() {
      this.handleQuery()
    },
    handleQuery() {
      this.pageNum = 1
      this.query()
    },
    handleExportExcel() {
      this.exportLoading = true
      let param = {}
      param = Object.assign({}, this.searchForm)
      ExaminationPackagesApi
        .queryByCond(param, 0, 0).then(rspData => {
          let datalist = []
          datalist = rspData.data.data.list
          import('@/vendor/Export2Excel').then(excel => {
            const tHeader = [
              '医院id',
              '性别',
              '年龄分组',
              '金额',
              '最大人数',
              '当前人数'
            ]
            const filterVal = [
              'hospitalId',
              'gender',
              'ageGroup',
              'amount',
              'maxSelection',
              'currentSelection'
            ]
            const list = datalist
            const data = this.formatJson(filterVal, list)
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: '导出Excel',
              autoWidth: this.autoWidth,
              bookType: this.bookType
            })
            this.exportLoading = false
          })
        }).catch(error => {
          this.exportLoading = false
          //console.log(error)
          if (error.message !== '') {
            //Message.error(error.message)
          }
        })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    query() {
      return new Promise((resolve, reject) => {
        this.queryLoading = true
        const param = Object.assign({}, this.searchForm)
        ExaminationPackagesApi
          .queryByCond(param, this.pageSize, this.pageNum)
          .then(response => {
            const data = response.data
            if (data.code === 200) {
              const data = response.data.data
              this.tableData = data
            } else {
              Message.error(data.message)
              reject()
            }
            this.queryLoading = false
          })
          .catch(error => {
            if (error.message !== '') {
              //Message.error(error.message)
            }
            this.queryLoading = false
            reject(error)
          })
      })
    },
    mockData() {
      setTimeout(() => {
        this.tableData = {
          total: 3, list: [
            {
              'hospitalId': '医院id-Data-1',
              'gender': '性别-Data-1',
              'ageGroup': '年龄分组-Data-1',
              'amount': '金额-Data-1',
              'maxSelection': '最大人数-Data-1',
              'currentSelection': '当前人数-Data-1',
            },
            {
              'hospitalId': '医院id-Data-2',
              'gender': '性别-Data-2',
              'ageGroup': '年龄分组-Data-2',
              'amount': '金额-Data-2',
              'maxSelection': '最大人数-Data-2',
              'currentSelection': '当前人数-Data-2',
            },
            {
              'hospitalId': '医院id-Data-3',
              'gender': '性别-Data-3',
              'ageGroup': '年龄分组-Data-3',
              'amount': '金额-Data-3',
              'maxSelection': '最大人数-Data-3',
              'currentSelection': '当前人数-Data-3',
            },
          ]
        }
      }, 1000)
    },
    handleAdd() {
      this.mode = 'add'
      this.currentRecord = {}
      this.showEditDialog = true
    },
    handleDetail(row) {
      this.mode = 'details'
      this.currentRecord = Object.assign({}, row)
      this.showEditDialog = true
    },
    handleEdit(row) {
      this.mode = 'edit'
      this.currentRecord = Object.assign({}, row)
      this.showEditDialog = true
    },

    // 处理翻页事件
    handlePageChange(currentPage) {
      this.pageNum = currentPage
      this.query()
    },
    // 处理页面总条数事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.query()
    },
    // 处理对话框关闭事件
    handleDialogClose(done) {
      this.$refs.editForm.resetFields()
      done()
    },
    handleDel(row) {
      this.$confirm('确定删除吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const param = Object.assign({}, row)
          ExaminationPackagesApi
            .delete(param)
            .then(response => {
              const data = response.data
              if (data.code === 200) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.handleQuery()
              } else {
                Message.error(data.message)
              }
            })
            .catch(error => {
              //console.log(error)
              if (error.message !== '') {
                //Message.error(error.message)
              }
            })
        }).catch(() => {
        })
    },
    // 处理编辑对话框关闭事件
    handleAfterEdit() {
      this.showEditDialog = false
      this.$refs.editForm.resetFields()
      this.query()
    }
  }
}
</script>
<style>
.container {
  margin: 10px;
}

.table-container {
  margin-top: 0px;
}

.pagination-container {
  margin-top: 30px;
  text-align: right;
}
</style>
